package com.xiaowu.netmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowu.netmusic.domain.dto.CommentAddDTO;
import com.xiaowu.netmusic.domain.po.Comment;
import com.xiaowu.netmusic.domain.po.CommentLike;
import com.xiaowu.netmusic.domain.po.UserPO;
import com.xiaowu.netmusic.domain.vo.CommentVO;
import com.xiaowu.netmusic.mapper.CommentLikeMapper;
import com.xiaowu.netmusic.mapper.CommentMapper;
import com.xiaowu.netmusic.service.CommentService;
import com.xiaowu.netmusic.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {


    private UserService userService;


    private final CommentLikeMapper commentLikeMapper;

    @Override
    public boolean addComment(CommentAddDTO dto) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(dto, comment);
        return this.save(comment);
    }


    @Override
    public IPage<CommentVO> getCommentPage(String targetType, Long targetId, int page, int size, Long currentUserId) {
        Page<Comment> commentPage = new Page<>(page, size);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("target_type", targetType).eq("target_id", targetId).orderByDesc("create_time");

        this.page(commentPage, wrapper);

        List<CommentVO> voList = commentPage.getRecords().stream().map(comment -> {
            CommentVO vo = new CommentVO();
            BeanUtils.copyProperties(comment, vo);

            UserPO user = userService.getById(comment.getUserId());
            if (user != null) {
                vo.setUsername(user.getUsername());
                vo.setAvatar(user.getAvatar());
            }

            // 点赞数量
            Long count = commentLikeMapper.selectCount(
                    new QueryWrapper<CommentLike>().eq("comment_id", comment.getId()));
            vo.setLikeCount(count.intValue());

            // 当前用户是否点赞
            if (currentUserId != null) {
                boolean isLiked = commentLikeMapper.selectCount(
                        new QueryWrapper<CommentLike>()
                                .eq("user_id", currentUserId)
                                .eq("comment_id", comment.getId())) > 0;
                vo.setIsLiked(isLiked);
            } else {
                vo.setIsLiked(false);
            }

            return vo;
        }).collect(Collectors.toList());

        Page<CommentVO> resultPage = new Page<>();
        BeanUtils.copyProperties(commentPage, resultPage, "records");
        resultPage.setRecords(voList);
        return resultPage;
    }



    @Override
    public boolean deleteComment(Long commentId, Long userId, boolean isAdmin) {
        Comment comment = this.getById(commentId);
        if (comment == null) {
            throw new RuntimeException("评论不存在");
        }
        if (!Objects.equals(comment.getUserId(), userId) && !isAdmin) {
            throw new RuntimeException("无权删除该评论");
        }
        return this.removeById(commentId);
    }
}
